﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RealNavigation : MonoBehaviour {

    public Transform guideCamera;
    public GuideHuman guideHuman;
    public Transform uiCamera;
    public Transform uiParent;
    private CustomDirectionsFactory directionsFactory;
    private List<Vector3> nodes;
    private int timeStep;
    private Vector3 currentNode;
    private bool isStart;

    // Use this for initialization
    void Start () {
        directionsFactory = FindObjectOfType<CustomDirectionsFactory>();
        nodes = new List<Vector3>();
        timeStep = 0;
        isStart = false;
    }

    private void FixedUpdate()
    {
        if(isStart)
        {
            timeStep++;
            if (timeStep >= 30)
            {
                timeStep = 0;
                JudgeDistance();
            }
        }
    }

    private void JudgeDistance()
    {
        if(Vector3.Distance(guideHuman.transform.position, guideCamera.position) < 20)
        {
            guideHuman.Move();
        }
        else
        {
            guideHuman.Pause();
        }
    }

    public void Begin()
    {
        nodes.Clear();
        SetGuideState(true);
        GetPositionNodes();
        guideHuman.Init(nodes);
        isStart = true;
    }

    private void GetPositionNodes()
    {
        Vector3 temp;
        foreach (var item in directionsFactory.guideTargetPoint)
        {
            temp = item;
            temp.y = transform.position.y;
            nodes.Add(temp);
        }
    }

    public void End()
    {
        SetGuideState(false);
        isStart = false;
    }

    private void SetGuideState(bool isShow)
    {
        guideCamera.gameObject.SetActive(isShow);
        guideHuman.gameObject.SetActive(isShow);
        uiCamera.gameObject.SetActive(!isShow);
        uiParent.gameObject.SetActive(!isShow);
    }
}
